package com.apop.accompany.bussiness.controller;

import com.alibaba.fastjson.JSONObject;
import com.apop.accompany.enums.ErrorCodeConstantsAccompany;
import com.apop.common.exception.ErrorCode;
import com.apop.common.exception.enums.GlobalErrorCodeConstants;
import com.apop.common.model.Result;
import com.apop.hrss.jwt.model.UserAuth;
import com.apop.hrss.jwt.service.AuthorizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 登陆校验
 * @ClassName AuthController
 * @Author Jiayi Zheng
 * @Date 2022/1/4 17:31
 * @Description: todo
 * @Version V1.0
 */
@RestController
@RequestMapping("/pz")
public class AdminLoginController {

    @Autowired
    AuthorizationService authorizationService;

    //用一个hashmap存储PC端注册用户 仅供演示
    private static ConcurrentHashMap<String, UserAuth> accountUserMap = new ConcurrentHashMap<>();


    static {

        // 默认创建一个用户
        UserAuth user = new UserAuth("admin","admin");
        //注册成功
        accountUserMap.put("admin",user);

    }

    /**
     * 注册
     * @param account
     * @param password
     * @return
     */
    @GetMapping("/register")
    public Result register(@RequestParam(name = "username") String account,
                           @RequestParam(name = "password") String password){
        //简单校验下非空
        if(account == null || account.equals("") || password == null || password.equals("")){
            return Result.fail(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR);
        }
        UserAuth user = new UserAuth(account,password);
        //注册成功
        accountUserMap.put(account,user);

        return Result.success(null);
    }

    /**
     * PC方式登录
     * @param requestData
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Map requestData){

        String account = (String)requestData.get("username");
        String password = (String)requestData.get("password");
        UserAuth user = accountUserMap.get(account);
        if(user != null && password != null && password.equals(user.getPassword())){

            //登录成功

            //创建token
            String token = authorizationService.createToken(user);

            //更新用户最近登录时间
            user.setLastLoginTime(new Date().toLocaleString());
            accountUserMap.put(account,user);
            Map data = new HashMap();
            data.put("token",token);
            return Result.success(data);
        }
        return Result.fail(ErrorCodeConstantsAccompany.AUTH_ERR_USER_NOT_FOUND);
    }

    /**
     * H5登录
     * @return
     */


    /**
     * token查询用户信息
     * @param token
     * @return
     */
    @GetMapping("/user")
    public Result queryUser(@RequestParam(name = "token") String token){
        JSONObject ret = new JSONObject();
        //识别token
        try {
            String account = authorizationService.verifyToken(token);
            if(account != null) {
                ret.put("user",accountUserMap.get(account).toString());
                return Result.success(ret);
            }else {
                //正常情况，token认证失败基本都是token过期，其他错误信息在上线前就调试正常了
                //token刷新问题，后面博文再做介绍
                return Result.fail(new ErrorCode(500,"token认证失败，请刷新token"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR);
    }

}
